import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:live_music/event/momonEvent.dart';
import 'package:live_music/models/tag_model.dart';
import 'package:live_music/services/tagService.dart';
import 'package:live_music/utils/eventBusUtils.dart';

class AddTabPage extends StatefulWidget {
  AddTabPage({Key key}) : super(key: key);

  @override
  _AddTabPageState createState() => _AddTabPageState();
}

class _AddTabPageState extends State<AddTabPage>
    with SingleTickerProviderStateMixin {
  TextEditingController _userNameEditController;
  @override
  void initState() {
    super.initState();
    _userNameEditController = TextEditingController();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: PreferredSize(
            preferredSize: Size.fromHeight(50),
            child: AppBar(
              elevation: 0.0,
              centerTitle: true,
              title: Center(
                child: Container(
                  alignment: Alignment.center,
                  child: Row(
                    mainAxisSize: MainAxisSize.max,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      GestureDetector(
                        child: Text(
                          "取消",
                          style: TextStyle(
                              fontSize: 16,
                              color: Colors.grey,
                              fontWeight: FontWeight.w500),
                        ),
                        onTap: () {
                          Get.back();
                        },
                      ),
                      Container(
                        child: Text(
                          "添加标签",
                          style: TextStyle(
                              fontSize: 16,
                              color: Colors.black,
                              fontWeight: FontWeight.w500),
                        ),
                      ),
                      InkWell(
                        child: Container(
                          child: Text(
                            "保存",
                            style: TextStyle(
                                fontSize: 16,
                                color: Colors.redAccent,
                                fontWeight: FontWeight.w500),
                          ),
                        ),
                        onTap: _addTabName,
                      )
                    ],
                  ),
                ),
              ),
              automaticallyImplyLeading: false,
            )),
        body: Column(children: [_buildName()]));
  }

  _addTabName() async {
    Map<String, dynamic> data = {"name": _userNameEditController.text};
    Map<String, dynamic> result = await TagService.addTab(data);
    TagModel tabModel = TagModel.fromJson(result['body']);
    EventBusUtils.getInstance().fire(AddTagEvent(tabModel));
    Get.back();
  }

  Widget _buildName() {
    return Container(
      margin: const EdgeInsets.only(top: 24, bottom: 12, left: 15, right: 15),
      height: 70,
      child: TextField(
        controller: _userNameEditController,
        onChanged: (value) {
          setState(() {
            // checkLoginText();
          });
        },
        cursorColor: Colors.redAccent,
        enableSuggestions: false,
        autocorrect: false,
        maxLength: 12,
        decoration: InputDecoration(
          hintText: "请输入标签名",
          filled: true,
          fillColor: Colors.white,
          contentPadding:
              const EdgeInsets.symmetric(vertical: 0, horizontal: 16),
          enabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.circular(20),
              borderSide: BorderSide(color: Colors.white, width: 2.0)),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(20),
            borderSide: BorderSide(
              color: Colors.white,
              width: 2,
            ),
          ),
        ),
      ),
    );
  }
}
